<template>
  <div id="shop-item">
    <div class="item-selector">
      <check-button :is-checked="product.checked" @click.native="checkClick" />
    </div>
    <div class="item-img">
      <img :src="product.image" alt="商品图片" />
    </div>
    <div class="item-info">
      <div class="item-title">{{ product.title }}</div>
      <div class="item-desc">{{ product.desc }}</div>
      <div class="info-bottom">
        <div class="item-price left">￥{{ product.price }}</div>
        <div class="item-price right">
          <button @click="decreaseCount">-</button>
          <span>×{{ product.count }}</span>
          <button @click="increaseCount">+</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton";
import { mapActions } from "vuex";

export default {
  name: "CartListItem",
  components: {
    CheckButton,
  },
  props: {
    product: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  /* created() {
    // 如果组件创建完就 表示购物车不为空
    this.$emit("isNull", false);
  }, */
  methods: {
    // 映射到actions.js里的deleteGoods函数
    ...mapActions(["deleteGoods"]),
    checkClick() {
      this.product.checked = !this.product.checked;
    },
    decreaseCount() {
      if (this.product.count === 1) {
        this.deleteGoods(this.product);
      }
      this.product.count--;
    },
    increaseCount() {
      this.product.count++;
    },
  },
};
</script>

<style scoped>
#shop-item {
  width: 100%;
  display: flex;
  font-size: 0;
  padding: 5px;
  border-bottom: 1px solid #ccc;
}

.item-selector {
  width: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item-title {
  overflow: hidden;
  /* 规定段落中的文本不换行 */
  white-space: nowrap;
  /* 溢出部分省略号 */
  text-overflow: ellipsis;
}

.item-desc {
  /* 两行显示 */
  overflow: hidden;
  /* 溢出部分省略号 */
  text-overflow: ellipsis;
  display: -webkit-box;
  /* 一行显示 */
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.item-img {
  flex: 2;
  padding: 5px;
  text-align: center;
}

.item-img img {
  width: 80%;
  display: inline-block;
  border-radius: 5px;
}

.item-info {
  flex: 6;
  position: relative;
  font-size: 17px;
  color: #333;
  padding: 5px 10px;
  overflow: hidden;
}

.item-info .item-desc {
  font-size: 14px;
  color: #666;
  margin-top: 15px;
}

.info-bottom {
  margin-top: 10px;
  position: absolute;
  bottom: 10px;
  left: 10px;
  right: 10px;
}

.info-bottom .left {
  float: left;
  font-size: 18px;
  color: orangered;
}
.info-bottom .right {
  float: right;
  font-size: 20px;
  color: #999;
}
.right span {
  margin: 0 5px;
}
.right button {
  background: #fff;
  font-size: 18px;
  text-align: center;
  padding: 0 6px;
  color: #666;
  border: 1px solid #ccc;
  border-radius: 50%;
}
</style>